package com.xiaobao.controller;

import com.xiaobao.pojo.Result;
import com.xiaobao.pojo.UserOnlineData;
import com.xiaobao.service.OnlineTimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

//学习在线时长统计
@RestController
public class OnlineTimeController {
    private static final Logger log = LoggerFactory.getLogger(OnlineTimeController.class);
    private final OnlineTimeService onlineTimeService;

    public OnlineTimeController(OnlineTimeService onlineTimeService) {
        this.onlineTimeService = onlineTimeService;
    }

    //添加在线时长工具
    @PostMapping("/userOnlineTime")
    public String receiveOnlineTime(@RequestBody UserOnlineData data) {
        String userId = data.getUserId();
        Long totalDuration = data.getTotalDuration();
        log.info("接收到用户{}的在线时长：{}ms", userId, totalDuration);
        onlineTimeService.addOnlineTime(userId, totalDuration);
        return "success";
    }
    //获取某个用户的学习时长
    //返回的数据是毫秒数转化为小时
    //然后统计每一天的数据
    //然后返回个数组给前端
    @GetMapping("/getUserOnlineTime/{userId}")
    public Result<UserOnlineData[]> getUserOnlineTime(@PathVariable String userId) {
        log.info("获取用户{}的在线时长", userId);
        UserOnlineData[] userOnlineTime = onlineTimeService.getUserOnlineTime(userId);
        log.info("用户{}的在线时长为：{}", userId, userOnlineTime);
        //返回数据
        return new Result<>( userOnlineTime);
    }
}
